<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class OrderController extends Controller
{
    public function add(){
    	$title = '订单管理';
    	return view('admin.order.add',['title'=>$title]);
    }

    public function insert(Request $request){
    	
    	$data = $request -> except(['_token']);
    	
    	//判断用户名是否为空
    	//laravel内置验证
    	$this ->validate($request,[
    		'uid'=>'required',
    		'id'=>'required',
    		'gid'=>'required',
    		'gnumber'=>'required',
    		'gunit'=>'required',
    		'rid'=>'required',
    		// 'address'=>'required',
    		// 'name'=>'required',
    		// 'phone'=>'required',
    		'ptatus'=>'required'
    		
    		],[
    		'uid.required'=>'关联用户id不能为空',
    		'id.required'=>'id不能为空',
    		'gid.required'=>'关联商品id不能为空',
    		
    		'gnumber.required'=>'商品的数量不能为空',
    		
    		'gunit.required'=>'规格不能为空',
    		'rid.required'=>'收货地址id不能为空',
    		// 'address.required'=>'收货地址不能为空',
    		// 'name.required'=>'收货人姓名不能为空',
    		// 'phone.required'=>'收货人手机号不能为空',
    		'ptatus.request'=>'支付状态不能为空']);
    		
    	//处理pic字段
    // 	if($request ->hasfile('logo'))
    // 	{
    // 		if($request ->file('logo')->isValid())
				// {
				// 	$suffix = $request->file('logo')->getClientOriginalExtension();
						//生成时间
    					date_default_timezone_set('PRC');
						$t = time();
						$otime = date('Y-m-d H:i:s',$t);
						
						$data['otime'] = $otime;
						$data['onum'] = $request->id.$t;
						
		
			//执行添加操作
			// dd($data);
			$res = DB::table('order')->insert($data);
			
			if($res)
			{
				return redirect('/admin/order');
			}else{
				return back();
			}  	

    }

    //index
    public function index(Request $request)
    {
   		// $data = DB::table('order') -> value('onum');
   		// dd($data);
    	//查询数据
    	$data =  DB::table('order')
    	// ->where('id',1);
         ->where('onum','like','%'.$request->input('keywords').'%')
         ->paginate($request->input('num',5));
         // ->paginate(5);
         
    	$title = '订单列表';
    	return view('admin.order.index',['title'=>$title,'data'=>$data,'request'=>$request->all()]);
    }

    public function edit($id)
    {

    	//查询数据
    	$data =DB::table('order') -> where('id',$id)->first(); 
    	$title = '订单编辑';

    	return view('admin.order.edit',['title'=>$title,'data'=>$data]);
    }
    

    	 public function update(Request $request){
    	 	// dd($request ->all());
    	 	$data = $request -> except(['_token']);
    	 	
    	 	//laravel内置验证
    	 	$id = $request ->input('id');

    	
			//执行添加操作
			$res = DB::table('order')->where('id',$id)->update($data);
			//
			if($res)
			{
				return redirect('/admin/order')->with(['info'=>'更新成功']);
			}else{
				return back();
			}  
    	 }
    	 public function delete($id){
    	 	$res= DB::table('order') ->where('id',$id)->delete();
    	 	if($res)
			{
				return redirect('/admin/order')->with(['info'=>'删除成功']);
			}else{
				return redirect('/admin/order')->with(['info'=>'删除不成功']);
			} 
    	 }
    	 // //ajax获取订单详情
    	 // public function ajaxreq(Request $request)
    	 // {
    	 // 	// dd($request ->all());
    	 // 	$onum = $request ->input('onum');
    	 // 	return response() ->json($onum);
    	 // }
	}

